package com.zyh.demo;

import com.zyh.domain.Student;

import java.io.*;

/**
 * @Author zyh
 * @Date 2024/8/6 14:49
 * @Description 对象流的读写
 */

public class IODemo5 {
    /*
    对象流的读，从文件中读到程序
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\Code\\IDEA\\idea_projects\\JavaSE-plus\\day10-code\\src\\com\\zyh\\resources\\Student.txt"));
        Object stu = ois.readObject();
        if (stu instanceof Student){
            Student student = (Student) stu;
            System.out.println(student);
        }
        ois.close();
    }


    /*
    对象流的写到文件中
     */
    public static void main1(String[] args) throws IOException {
        Student stu1 = new Student("zyh", 20);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\Code\\IDEA\\idea_projects\\JavaSE-plus\\day10-code\\src\\com\\zyh\\resources\\Student.txt"));
        oos.writeObject(stu1);
        oos.flush();
        oos.close();
    }
}
